package com.atguigu.java.ai.langchain4j.config;

import dev.langchain4j.community.store.embedding.redis.RedisEmbeddingStore;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author huoyz
 * @Date 2025/5/10 22:51
 * @Version 1.0
 * @description: https://redis.io/
 * Redis Stack 的向量存储
 */
@Configuration
public class EmbeddingStoreConfig {
    @Autowired
    private EmbeddingModel embeddingModel;
    @Value("${spring.data.redis.host}")
    private String redisHost;

    @Value("${spring.data.redis.port}")
    private int redisPort;

    @Value("${spring.data.redis.password}")
    private String redisPassword;

    @Bean
    public EmbeddingStore<TextSegment> embeddingStore() {
        // 创建 Redis Stack 向量存储
        EmbeddingStore<TextSegment> embeddingStore = RedisEmbeddingStore.builder()
                .host(redisHost) // Redis 服务器地址
                .port(redisPort) // Redis 服务器端口
                .password(redisPassword)
                .indexName("xiaozhi-index") // 指定向量索引名称
                .dimension(embeddingModel.dimension()) // 指定向量维度，该维度与 embeddedModel 生成的向量维度相同
                .build();
        return embeddingStore;
    }


}